package com.qyhl.module_practice.volunteer.sign;

import android.annotation.SuppressLint;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.qyhl.module_practice.common.PracticeUrl;
import com.qyhl.webtv.basiclib.utils.DESedeUtil;
import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.basiclib.utils.network.model.ApiResult;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.entity.UpTokenBean;
import com.qyhl.webtv.commonlib.entity.civilized.PracticeListBean;
import com.qyhl.webtv.commonlib.entity.civilized.PracticeSignBean;
import com.qyhl.webtv.commonlib.entity.civilized.PracticeSignTagBean;

import java.lang.reflect.Type;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class PracticeVolunteerSignModel implements PracticeVolunteerSignContract.PracticeVolunteerSignModel {
    private PracticeVolunteerSignPresenter mPresenter;

    PracticeVolunteerSignModel(PracticeVolunteerSignPresenter mPresenter) {
        this.mPresenter = mPresenter;
    }


    @Override
    public void getTagList() {

        EasyHttp.get(PracticeUrl.GET_VOLUNTEER_OPTION)
                .params("siteId", CommonUtils.getInstance().getSite() + "")
                .execute(new SimpleCallBack<PracticeSignBean>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.getTagFailure("获取服务类型失败！");
                    }

                    @Override
                    public void onSuccess(PracticeSignBean practiceSignBean) {
                        mPresenter.getTagSuccess(practiceSignBean);
                    }
                });

    }

    @Override
    public void getInstList(String instId) {
        EasyHttp.get(PracticeUrl.GET_STREET_SIMPLE_LIST)
                .params("instId", instId)
                .execute(new SimpleCallBack<List<PracticeListBean>>() {
                    @Override
                    public void onError(ApiException e) {
                        if (e.getCode() == 202) {
                            mPresenter.setError("暂无任何内容！");
                        } else {
                            mPresenter.setError("机构获取失败，请重新尝试！");
                        }
                    }

                    @Override
                    public void onSuccess(List<PracticeListBean> practiceListBeans) {
                        if (practiceListBeans != null && practiceListBeans.size() > 0) {
                            mPresenter.setList(practiceListBeans);
                        } else {
                            mPresenter.setError("暂无任何机构！");
                        }
                    }
                });
    }

    @Override
    public void doSignForPublic(String name, String phone, String sex, String actId) {
        EasyHttp.post(PracticeUrl.SIGN_FOR_PUBLIC)
                .params("name", name)
                .params("phone", phone)
                .params("sex", sex)
                .params("actId", actId)
                .params("siteId", CommonUtils.getInstance().getSite() + "")
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.signFailure(null, "提交失败，请重新尝试！");
                    }

                    @Override
                    public void onSuccess(String s) {
                        ApiResult apiResult = new Gson().fromJson(s, ApiResult.class);
                        if (apiResult.getCode() == 200) {
                            mPresenter.signSuccess("报名成功！");
                        } else {
                            mPresenter.signFailure(null, "提交失败，请重新尝试！");
                        }
                    }
                });
    }

    @Override
    public void doSignForVolunteer(String sex, String permanentAddress, String politicalFace, String name, String phone,
                                   String logo, String tagIds, String orgIds, String orgName, String orgDes, String orgInsId,
                                   String orgType, String orgCon, String workAddress, String idCard) {
        EasyHttp.post(PracticeUrl.SIGN_FOR_VOLUNTEER)
                .params("siteId", CommonUtils.getInstance().getSite() + "")
                .params("sex", sex)
                .params("permanentAddress", permanentAddress)
                .params("politicalFace", politicalFace)
                .params("name", name)
                .params("phone", phone)
                .params("logo", logo)
                .params("tagIds", tagIds)
                .params("orgIds", orgIds)
                .params("orgName", orgName)
                .params("orgDes", orgDes)
                .params("orgInsId", orgInsId)
                .params("orgType", orgType)
                .params("orgCon", orgCon)
                .params("workAddress", workAddress)
                .params("card", idCard)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.signFailure(null, "提交出错，请重新尝试！");
                    }

                    @Override
                    public void onSuccess(String s) {
                        ApiResult apiResult = new Gson().fromJson(s, ApiResult.class);

                        if (apiResult.getCode() == 200) {
                            if (apiResult.getData().equals("0")) {
                                mPresenter.signSuccess("恭喜你，已成功注册成为志愿者！");
                            } else {
                                mPresenter.signSuccess("志愿者申请提交成功，请等待审核！");
                            }
                        } else if (apiResult.getCode() == 305 || apiResult.getCode() == 306) {
                            Type type = new TypeToken<List<PracticeVolunteerSignActivity.ContractBean>>() {
                            }.getType();
                            List<PracticeVolunteerSignActivity.ContractBean> beanList = new Gson().fromJson(apiResult.getData().toString(), type);
                            mPresenter.signFailure(beanList, apiResult.getMessage());
                        } else {
                            mPresenter.signFailure(null, "提交失败，请重新尝试！");
                        }
                    }
                });


    }

    @Override
    public void getToken(final boolean isUpload) {
        @SuppressLint("SimpleDateFormat") SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String time = sdf.format(new Date());
        String code = "Z5" + time + "I2863";
        String url = CommonUtils.getInstance().getCircleUrl() + "?timestamp=" + time;

        EasyHttp.post(url)
                .upBytes(DESedeUtil.encryptMode(code, "siteId=" + CommonUtils.getInstance().getSite() + "&method=getUpToken&type=3"))
                .isDecrypt(true)
                .decryptKet(code)
                .execute(new SimpleCallBack<UpTokenBean>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.getTokenError(isUpload);
                    }

                    @Override
                    public void onSuccess(UpTokenBean upTokenBean) {
                        mPresenter.getTokenSuccess(upTokenBean, isUpload);
                    }
                });

    }

    @Override
    public void getTeamTag() {

        EasyHttp.get(PracticeUrl.GET_PRACTICE_TEAM_TAG)
                .params("siteId", CommonUtils.getInstance().getSite() + "")
                .execute(new SimpleCallBack<List<PracticeSignTagBean>>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.getTeamTagError("获取服务类型失败！");
                    }

                    @Override
                    public void onSuccess(List<PracticeSignTagBean> practiceSignTagBeans) {
                        mPresenter.getTeamTagSuccess(practiceSignTagBeans);
                    }
                });
    }
}
